﻿@model OT.Areas.Admin.Models.Topics
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
    <h3 id="myModalLabel">Create Topic</h3>
</div>
<div class="modal-body">
    <div id="alert_modal">
        @Html.ValidationSummary()
    </div>
    <div class="form-horizontal" role="form">
        <div class="form-group">
            <div class="col-lg-15">
                <div id="divMsgCre"></div>
            </div>
        </div>
        <div class="form-group" id="DivCategoryName">
            <label class="col-lg-3 control-label">Category Name</label>
            <div class="col-lg-5">
                @Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.category, "CategoryID", "CategoryName"), "----Select----", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CategoryID)
            </div>
        </div>
        <div class="form-group" id="DivSubjectName">
            <label class="col-lg-3 control-label">Subject Name</label>
            <div class="col-lg-5">
                <select id="SubjectID" class="form-control">
                    <option value="0">---Select------</option>
                </select>
            </div>
        </div>
        <div class="form-group" id="divtopic">
            <label class="col-lg-3 control-label">Topic Name</label>
            <div class="col-lg-5">
                <input class="form-control" type="text" placeholder="Enter Topic Name" id="TopicName">
            </div>
        </div>
        <div class="form-group" id="divdescription">
            <label class="col-lg-3 control-label">Description</label>
            <div class="col-lg-5">
                <textarea class="form-control" placeholder="Enter Description" id="Description" rows="3"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 control-label">Status</label>
            <div class="col-lg-1">
                <input class="form-control" id="st" name="st" type="checkbox">
            </div>
        </div>
        @*<div class="form-group" id="DivUser">
            <label class="col-lg-3 control-label">User Name</label>
            <div class="col-lg-5">
                @Html.DropDownListFor(model => model.UserID, new SelectList(Model.Users, "UserID", "FirstName"), "--Select--", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.UserID)
            </div>
        </div>*@
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-default" type="submit" id="btnCreate"><i class="icon-cog"></i>Save</button>
    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i>Cancel</button>
</div>
<script type="text/javascript">
    $("#btnCreate").click(function (e) {
        var TopicName = $("#TopicName").val();
        var Description = $("#Description").val();
        var Category = $("#CategoryID").val();
        var user = 0;
        if ($("#st").is(':checked')) {
            Status = true;
        }
        else {
            Status = false;
        }
        var SubjectID = $("#SubjectID").val();
        var UserID = $("#UserID").val();
        e.preventDefault();
        if (TopicName == '' || SubjectID == 0 ||Category==0) {
            showMsg('divMsgCre', 'Please Enter Required Value', 'danger');
            (TopicName == '') ? $('#divtopic').addClass('has-error') : $('#divtopic').removeClass('has-error');
            (Category == 0) ? $('#DivCategoryName').addClass('has-error') : $('#DivCategoryName').removeClass('has-error');
            (SubjectID == 0) ? $('#DivSubjectName').addClass('has-error') : $('#DivSubjectName').removeClass('has-error');
            //(user == 0) ? $('#DivUser').addClass('has-error') : $('#DivUser').removeClass('has-error');
        }
        else {
            $.ajax({
                url: "/Admin/Topic/Create?TopicName=" + TopicName + "&SubjectID=" + SubjectID + "&Description=" + Description + "&Status=" + Status + "&UserID=" + UserID,
                type: "POST",
                success: function (result) {
                    if (result != "NotValid") {
                        $('#modalDiv').modal('hide');
                        showMsg('divMsgIndex', 'Record Created Successfuly', 'success');
                        loadPage(1, 10, '');
                    }
                    else {
                        showMsg('divMsgCre', 'Record Not Saved !!', 'danger');
                    }
                },
                error: function (data) {
                }
            });
        }
        return false;
    });

    $("#CategoryID").change(function (e) {
        var CategoryID = $(this).val();
        var sub = $("#SubjectID").empty();
        $.getJSON("/Admin/Topic/LoadSubject", { CategoryID: CategoryID },
               function (classesData) {
                   var select = $("#SubjectID");
                   select.empty();
                   select.append($('<option/>', {
                       value: 0,
                       text: "----Select Subject----"
                   }));
                   $.each(classesData, function (index, itemData) {

                       select.append($('<option/>', {
                           value: itemData.Value,
                           text: itemData.Text
                       }));
                   });
               });
        return false;
    });

    $("#SubjectID").change(function (e) {
        var SubjectID = $(this).val();
        var sub = $("#TopicID").empty();
        $.getJSON("/Admin/Question/LoadTopic", { SubjectID: SubjectID },
               function (classesData) {
                   var select = $("#TopicID");
                   select.append($('<option/>', {
                       value: 0,
                       text: "----Select Topic----"
                   }));
                   $.each(classesData, function (index, itemData) {

                       select.append($('<option/>', {
                           value: itemData.Value,
                           text: itemData.Text
                       }));
                   });
               });
        return false;
    });
</script>
